package view;
import javax.imageio.ImageIO;
import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Vector;

/**
 * Represents the GUI part of the system, integrating both
 * a login screen as well as a workspace screen.
 * 
 * @author Kenny Chong, Dan End, James Ju
 * CSE 333 : User Interface
 * Project HMSystem
 */
public class HMSView extends JFrame
{
	// Variables.
	private LoginPanel loginPanel;
	private WorkSpacePanel workSpacePanel;
	private MessagePanel messagePanel;
	
	//Our JMenu Bar & Toolbar.
	private JMenuBar menuBar;
	
	// For the login.
	private JMenu optionLoginMenu;
	private JMenu helpLoginMenu;
	
	private JMenuItem exitItem;
	private JMenuItem helpItem;
	private JMenuItem aboutItem;
	
	// For the workspace.
	private JMenu optionWSMenu;
	
	private JToolBar toolBar;
	
	//Options Menu
	private JMenu option;
	private JMenuItem department;
	private JMenuItem preference;
	private JMenuItem logout;
	
	//Messages Menu
	private JMenu messages;
	private JMenuItem log;
	private JMenuItem recentmsgs;
	
	//Management Menu
	private JMenu management;
	private JMenuItem employees;
	private JMenuItem budget;
	private JMenuItem floors;
	
	//Help Menu (for work space).
	private JMenu help;
	private JMenuItem aboutUs;
	private JMenuItem getHelp;

	//Buttons
	private JButton employeeButton;
	private JButton budgetButton;
	private JButton floorButton;
	private JButton messageButton;
	private JButton homeButton;
	
	// Vector Images for ToolBar
	private Vector<Image> toolBarIMG;
	
	private BufferedImage image;
	private BackgroundPanel bgPanel;
	private JPanel middlePanel;
	
	// Layout.
	CardLayout layout;
	
	/**
	 * Class constructor.
	 */
	public HMSView()
	{					
		// Initialize the Frame.
		this.init();	
	}
	
	/**
	 * Initialize the GUI.
	 * @throws IOException 
	 */
	public void init()
	{
		// Set title.
		this.setTitle("Hotel Management System");
		
		// Initialize middlePanel.
		middlePanel = new JPanel();

		// Set card layout to middlePanel.
		layout = new CardLayout();
		middlePanel.setLayout(layout);
		
		// Read image.
		try 
		{
			image = ImageIO.read(new File(
					"images/Background-sky-blue-waves.jpg"));
		} catch (IOException e) {
			e.printStackTrace();
		}

		// Initialize Background panel.
		bgPanel = new BackgroundPanel(image, 0);

		// Initialize LoginPanel.
		loginPanel = new LoginPanel(this);

		// Initialize WorkSpacePanel.
		workSpacePanel = new WorkSpacePanel(this);
		
		// Add loginPanel to background panel.
		bgPanel.add(loginPanel);
		
		// Add background panel with login.
		middlePanel.add(bgPanel, "login");
		
		// Add login menu bar at first.
		this.initLoginMenuBar();
		
		// Add workspace pane to middlePanel.
		middlePanel.add(workSpacePanel, "workspace");
		
		// Add middle panel to frame.
		this.add(middlePanel, BorderLayout.CENTER);
		
		// Set frame location to center.
		this.setLocationRelativeTo(null);
		
		// Set size.
		this.setExtendedState(this.getExtendedState()|JFrame.MAXIMIZED_BOTH);
	
		// Set close operation.
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	/**
	 * Initialize the login menu bar and its menu items. 
	 */
	public void initLoginMenuBar()
	{	
		// Initialize menubar.
		menuBar = new JMenuBar();
		
		// Initialize the menu.
		optionLoginMenu = new JMenu("Options");
		helpLoginMenu = new JMenu("Help");
		
		// Initialize menu items.
		exitItem = new JMenuItem("Exit");
		exitItem.setAccelerator(KeyStroke.getKeyStroke(
		        KeyEvent.VK_E, ActionEvent.CTRL_MASK));
		
		helpItem = new JMenuItem("Help");
		helpItem.setAccelerator(KeyStroke.getKeyStroke(
		        KeyEvent.VK_H, ActionEvent.CTRL_MASK));
		aboutItem = new JMenuItem("About");
		aboutItem.setAccelerator(KeyStroke.getKeyStroke(
		        KeyEvent.VK_A, ActionEvent.CTRL_MASK));
		
		// Add menus it to menu bar.
		menuBar.add(optionLoginMenu);
		menuBar.add(helpLoginMenu);
		
		// Add menu items to menu bar.
		optionLoginMenu.add(exitItem);
		helpLoginMenu.add(helpItem);
		helpLoginMenu.add(aboutItem);
		this.setJMenuBar(menuBar);
	}
	
	/**
	 * Initialize the work space 'tool bar'.
	 */
	public JToolBar initWSToolBar()
	{
		JToolBar bar = new JToolBar();

		// Initialize Toolbar Images
		try 
		{
			initToolBarImage();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// Initialize JButton
		messageButton = new JButton(new ImageIcon(toolBarIMG.elementAt(0)));
		budgetButton = new JButton(new ImageIcon(toolBarIMG.elementAt(1)));
		employeeButton = new JButton(new ImageIcon(toolBarIMG.elementAt(2)));
		floorButton = new JButton(new ImageIcon(toolBarIMG.elementAt(3)));
		homeButton = new JButton(new ImageIcon(toolBarIMG.elementAt(4)));
		 
		// Initialize the tool bar.
		bar = new JToolBar();

		// Add Buttons to Tool Bar
		bar.add(homeButton);
		bar.add(messageButton);
		bar.add(employeeButton);
		bar.add(floorButton);
		bar.add(budgetButton);
		bar.setVisible(true);

		return bar;
	}
	
	/**
	 * Initialize work space 'menu bar'.
	 */
	public void initWSMenuBar()
	{
		//Initialize MenuItems
		department = new JMenuItem("Department");
		preference = new JMenuItem("Preference");
		logout = new JMenuItem("Log Out");
		log = new JMenuItem("Log");
		recentmsgs = new JMenuItem("Recent Messages");
		employees = new JMenuItem("Employees");
		floors = new JMenuItem("Floors");
		budget = new JMenuItem("Budget");
		aboutUs = new JMenuItem("About Us");
		getHelp = new JMenuItem("Help");
		
		//Initialize menus;
		option = new JMenu("Options");
		messages = new JMenu("Messages");
		management = new JMenu("Management");
		help = new JMenu("Help");
		
		//Add MenuItems to Menus
		option.add(department);
		option.add(preference);
		option.add(logout);
		messages.add(log);
		messages.add(recentmsgs);
		management.add(employees);
		management.add(budget);
		management.add(floors);
		help.add(aboutUs);
		help.add(getHelp);
		
		// Initialize the menu bar.
		menuBar = new JMenuBar();
		
		//Add Menus to Menubar
		menuBar.add(option);
		menuBar.add(messages);
		menuBar.add(management);
		menuBar.add(help);
		
		this.setJMenuBar(menuBar);
	}
	
	/**
	 * Get LoginPanel.
	 */
	public LoginPanel getLoginPanel()
	{
		return loginPanel;
	}
	
	/**
	 * Used to return the main WorkSpace. 
	 */
	public WorkSpacePanel getWorkSpacePanel()
	{
		return workSpacePanel;
	}
	
	/**
	 * Tool Bar images will be rescaled and loaded into a vector
	 * @throws IOException 
	 * 
	 */
	public void initToolBarImage() throws IOException
	{
		Image img;
		Image scaleimg;
		toolBarIMG = new Vector<Image>();
		
		//First call the Image
		img = ImageIO.read(new File("images/message.jpg"));
		//Scale it
		scaleimg = img.getScaledInstance( 60,40,  java.awt.Image.SCALE_SMOOTH );
		//Save it to the Image Vector
		toolBarIMG.add(scaleimg);
		
		//Do the same procedure for all the images
		img = ImageIO.read(new File("images/budget.jpg"));
		scaleimg = img.getScaledInstance( 60,40,  java.awt.Image.SCALE_SMOOTH );
		toolBarIMG.add(scaleimg);
		img = ImageIO.read(new File("images/employee.jpg"));
		scaleimg = img.getScaledInstance( 60,40,  java.awt.Image.SCALE_SMOOTH );
		toolBarIMG.add(scaleimg);
		img = ImageIO.read(new File("images/floorlayer.png"));
		scaleimg = img.getScaledInstance( 60,40,  java.awt.Image.SCALE_SMOOTH );
		toolBarIMG.add(scaleimg);
		
        img = ImageIO.read(new File("images/home.png"));
        scaleimg = img.getScaledInstance( 60,40,  java.awt.Image.SCALE_SMOOTH );
        toolBarIMG.add(scaleimg);
	}
	
	/**
	 * Open the frame. 
	 */
	public void open()
	{
		this.setVisible(true);
	}
	
	/**
	 * Switch between different panels.
	 */
	public void setPanelSpace(PanelType type)
	{	
		// Determine type.
		switch (type)
		{
		case LOGIN:
			// Reset password.
			loginPanel.reset();
			
			// Set to login menu.
			layout.show(middlePanel, "login");
			
			// Initialize login menu bar.
			this.initLoginMenuBar();
			
			if (toolBar != null)
			{
				// Disable tool bar, if any.
				toolBar.setVisible(false);
			}

			break;
		case WORK:
			// Set to workspace menu.
			layout.show(middlePanel, "workspace");
	
			// Reset to home page.
			workSpacePanel.setPanel("homepagePanel");
			
			// Initialize workspace menu bar.
			this.initWSMenuBar();
			
			if (toolBar != null)
			{
				// Enable tool bar.
				toolBar.setVisible(true);
			} else {
				// Initialize tool bar.
				toolBar = this.initWSToolBar();
				
				// Add toolbar.
				this.add(toolBar, BorderLayout.NORTH);
			}
		
			break;
		}
	}
	
	// Listeners.
	public void setExitListener(Action e)
	{
		exitItem.setAction(e);
	}
	
	// For Login Components.
	// Submit button.
	public void setSubmitListener(ActionListener e)
	{
		LoginBox.getLoginBox().getSubmitButton().addActionListener(e);
	}
	
	/**
	 * Set Action Listener for help button and menu item.
	 * @param e
	 */
	public void setHelpListener(Action e)
	{
		LoginBox.getLoginBox().getHelpButton().setAction(e);
		helpItem.setAction(e);
	}
	
	/**
	 * Set action listener for about menu item.
	 */
	public void setAboutListener(Action e)
	{
		aboutItem.setAction(e);
	}
	
	/**
	 * Set Action Listener for departments.
	 * @param lis
	 */
	public void addActionListenerDepartment(ActionListener lis)
	{
		department.addActionListener(lis);
	}
	
	/**
	 * Set Action Listener for Preference
	 * @param lis
	 */
	public void addActionListenerPreference(ActionListener lis)
	{
		preference.addActionListener(lis);
	}
	
	/**
	 * Set Action Listener for LogOut
	 * @param lis
	 */
	public void addActionListenerLogOut(ActionListener lis)
	{
		logout.addActionListener(lis);
	}
	
	/**
	 * Set Action Listener for Log
	 * @param lis
	 */
	public void addActionListenerLog(ActionListener lis)
	{
		log.addActionListener(lis);
	}
	
	/**
	 * Set Action Listener for recentmsgs
	 * @param lis
	 */
	public void addActionListenerRecentMsgs(ActionListener lis)
	{
		recentmsgs.addActionListener(lis);
		messageButton.addActionListener(lis);
	}
	
	/**
	 * Set Action Listener for Employee
	 * @param lis
	 */
	public void addActionListenerEmployee(ActionListener lis)
	{
		employees.addActionListener(lis);
		employeeButton.addActionListener(lis);
	}
	
	/**
	 * Set Action Listener for budget
	 * @param lis
	 */
	public void addActionListenerBudget(ActionListener lis)
	{
		budget.addActionListener(lis);
		budgetButton.addActionListener(lis);
	}
	/**
	 * Set Action Listener for About Us 
	 * @param lis
	 */
	public void addActionListenerAboutUs(ActionListener lis)
	{
		aboutUs.addActionListener(lis);
	}
	/**
	 * Set Action Listener for GetHelp
	 * @param lis
	 */
	public void addActionListenerGetHelp(ActionListener lis)
	{
		getHelp.addActionListener(lis);
	}
	
	/**
	 * Set Action Listener for Floor
	 * @param lis
	 */
	public void addActionListenerFloor(ActionListener lis)
	{
		floors.addActionListener(lis);
		floorButton.addActionListener(lis);
	}

	/**
     * Set Action Listener for Home
     * @param lis
     */
    public void addActionListenerHome(ActionListener lis)
    {
        homeButton.addActionListener(lis);
    }

	// Please feel free to incorporate any other methods as required.
}
